const chunks = require('array.chunk');

// [[1, 2], [3, 4], [5]]
console.log(chunks([1, 2, 3, 4, 5], 2));
console.log(arrayChunk([1, 2, 3, 4, 5], 2));


function arrayChunk() {
	return arrayChunkV1.apply(null, arguments)
}


function arrayChunkV1(arr, size) {
	if (!Array.isArray(arr)) {
	    throw new TypeError('Input should be Array or TypedArray');
	}

	if (typeof size !== 'number') {
	    throw new TypeError('Size should be a Number');
	}

	var result = [], tmp = []

	for (var i = 0; i < arr.length; i++) {
		tmp.push(arr[i])

		if((i + 1) % size === 0) {
			result.push(tmp)
			tmp = []
		}
	}

	if(tmp.length) {
		result.push(tmp)
	}

	return result
}